package excel.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import excel.file.FileTypeUtil;
import excel.file.MimeTypeUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.http.MediaType;

/**
 * 文件处理工具类
 */
public class FileUtil extends org.apache.commons.io.FileUtils {

    public static String TEMP_DIRECTORY = "/temp_dir/";

    public static String TEMP_DIRECTORY_FOR_MAC = System.getProperty("user.home") + "/temp_dir/";

    public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";

    /**
     * 输出指定文件的byte数组
     *
     * @param filePath 文件路径
     * @param os       输出流
     *
     * @return
     */
    public static void writeBytes(String filePath, OutputStream os) throws IOException {
        FileInputStream fis = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                throw new FileNotFoundException(filePath);
            }
            fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int length;
            while ((length = fis.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除文件
     *
     * @param filePath 文件
     *
     * @return
     */
    public static boolean deleteFile(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

    /**
     * 文件名称验证
     *
     * @param filename 文件名称
     *
     * @return true 正常 false 非法
     */
    public static boolean isValidFilename(String filename) {
        return filename.matches(FILENAME_PATTERN);
    }

    /**
     * 检查文件是否可下载
     *
     * @param resource 需要下载的文件
     *
     * @return true 正常 false 非法
     */
    public static boolean checkAllowDownload(String resource) {
        // 禁止目录上跳级别
        if (StringUtils.contains(resource, "..")) {
            return false;
        }

        // 检查允许下载的文件规则
        if (ArrayUtils.contains(MimeTypeUtil.DEFAULT_ALLOWED_EXTENSION, FileTypeUtil.getFileType(resource))) {
            return true;
        }

        // 不在允许下载的文件规则
        return false;
    }

    /**
     * 下载文件名重新编码
     *
     * @param request  请求对象
     * @param filename 文件名
     *
     * @return 编码后的文件名
     */
    public static String encodeByAgent(HttpServletRequest request, String filename)
            throws UnsupportedEncodingException {
        final String agent = request.getHeader("USER-AGENT");
        if (agent.contains("MSIE")) {
            // IE浏览器
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
        } else if (agent.contains("Firefox")) {
            // 火狐浏览器
            filename = new String(filename.getBytes(), "ISO8859-1");
        } else if (agent.contains("Chrome")) {
            // google浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        } else {
            // 其它浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;
    }

    /**
     * 下载文件名重新编码
     *
     * @param response 响应对象
     * @param filename 真实文件名
     *
     * @return
     */
    public static void setAttachmentResponseHeader(HttpServletResponse response, String filename)
            throws UnsupportedEncodingException {
        filename = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());

        //响应头 Content-Disposition:attachment;filename=a.xls
        StringBuilder contentDispositionValue = new StringBuilder();
        contentDispositionValue.append("attachment;")
                .append("filename=")
                .append(filename);

        response.setHeader("Content-disposition", contentDispositionValue.toString());
        response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
    }

    public static void downloadFileOnce(String filename, HttpServletResponse response) throws Exception {
        downloadFile(filename, response, true);
    }

    public static void downloadFile(String filename, HttpServletResponse response, Boolean delete) throws Exception {
        if (!checkAllowDownload(filename)) {
            throw new RuntimeException(String.format("文件名称(%s)非法，不允许下载。 ", filename));
        }
        String respFilename = filename.substring(filename.indexOf("_") + 1, filename.indexOf("."))
                + DateFormatUtils.format(new Date(), "yyyyMMddHHmmsss")
                + filename.substring(filename.indexOf("."));
        String filePath = TEMP_DIRECTORY + filename;
        if (System.getProperties().getProperty("os.name").startsWith("Mac")) {
            filePath = TEMP_DIRECTORY_FOR_MAC + filename;
        }

        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        setAttachmentResponseHeader(response, respFilename);
        writeBytes(filePath, response.getOutputStream());
        if (delete) {
            deleteFile(filePath);
        }
    }
}
